package com.exam.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.exam.dao.SettingDao;
import com.exam.model.SettingModel;


/**
 * 系统初始化
 * @author 刘楠
 *
 */
public class InitService implements ServletContextListener {    
	public Connection conn= null;
	public PreparedStatement pstm = null;
	public ResultSet res,res2 = null;

	/**
	 * 系统退出时调用
	 */
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("web exit...");
		//保存系统设置
		new SettingDao().updateSetting((SettingModel) sce.getServletContext().getAttribute("SYS_SETTING"));
	}

	/**
	 * 系统加载时 初始化
	 */
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("web init...");
		System.out.println("初始化系统设置...");
		ServletContext application=sce.getServletContext();  
		try {
			
			SettingModel setting = new SettingDao().getSystemSetting();
			application.setAttribute("SYS_SETTING", setting);
			
		} catch (SQLException e) {
			System.out.println("ERROR:初始化系统参数错误...");
			e.printStackTrace();
		}
	}    
}    